package com.aponisipsis.gl3d.render;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;

import com.aponisipsis.gl3d.geometry.Object3D;

public class Camera extends Object3D{
	public final static int Ortographic=1;
	public final static int Perspective=2;
	float angle=0;
	float eyex, eyey, eyez;
	float rx, ry, rz;
	float cx, cy, cz;
	
	float yaw, pitch, roll;
	float fov,aspect,zNear,zFar;
	
	int mode;
	
	public Camera(int mode){
		this.mode=mode;
		this.setPosition(new Vector3f(0,0,0));
		cx=cy=cz=0;
		rx=ry=rz=0;
		angle=0;
		//zNear = -10.0f;
		//zFar = 10.0f;
	}
	
	public void translate(Vector3f position){
		
	}
	
	public void lookAt(float x, float y, float z){
		this.cx=x;
		this.cy=y;
		this.cz=z;
	}
	
//	public void move(float dx, float dy, float dz){
//		this.eyex+=dx;this.cx+=dx;
//		this.eyey+=dy;this.cy+=dy;
//		this.eyez+=dz;this.cz+=dz;
//	}
//	
//	public void rotate(float angle, float x, float y, float z){
////		Matrix4f tmp = new Matrix4f();
////		Vector3f negPos = new Vector3f(this.position);
////		System.out.println(negPos);
////		negPos.negate();
////		tmp.translate(negPos);
////		tmp.rotate(angle, new Vector3f(x,y,z));
////		tmp.translate(position);
//////		tmp.translate(this.position);
////		Vector4f xx = Matrix4f.transform(tmp, new Vector4f(cx,cy,cz,1), null);
////		cx=xx.x;cy=xx.y;cz=xx.z;
////		//System.out.println(lookAt);
////		//cx=lookAt.x;cy=lookAt.y;cz=lookAt.z;
//		this.angle+=angle;
//	}

	public void rotate(float yaw, float pitch, float roll){
		this.yaw+=yaw;
		this.pitch+=pitch;
		this.roll+=roll;
	}
	
	public void setPerspective(float fov, float aspect,float zNear, float zFar){
		if(mode==Perspective){
			this.fov=fov;
			this.aspect=aspect;
			this.zNear=zNear;
			this.zFar=zFar;
		}
	}
	public void render() {
		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		//GL11.glTranslatef(position.x, position.y, position.z);
		cx=3;
		if(mode==Ortographic){
			GLU.gluOrtho2D(-Display.getWidth()/2,Display.getWidth()/2,-Display.getHeight()/2,Display.getHeight()/2);
		}else{
			GLU.gluPerspective(fov,aspect,zNear,zFar);
			GLU.gluLookAt(position.x,position.y,position.z,cx,cy,cz,0,1,0);
		}		
		GL11.glRotatef(yaw, 0, 1, 0);
		GL11.glRotatef(pitch, 0, 0, 1);
		GL11.glRotatef(roll, 1, 0, 0);
	}
}
